/*全选按钮*/
$(function () {
	/*单个点击事件*/
	$(".mz-checkbox").click(function () {
		var box = $(this).attr("id");
		var cartnumber = box.substring(4, box.length);
        var checked = 0;
		if ($(this).hasClass("checked")) {
            $(this).removeClass("checked");
            checked = 0;
		} else {
            $(this).addClass("checked");
            checked = 1;
		}

		var count = $("#sum" + cartnumber).html();
		let html = $("#totalPrice").html();
		if ($(this).hasClass("checked")) {
			$("#totalPrice").html(parseInt(html) + parseInt(count));
		} else {
			$("#totalPrice").html(parseInt(html) - parseInt(count));
		}
		$(".totalSelectedCount").html($(".checked").length);
        var c = $("#count" + cartnumber).val();
        upCart(c, cartnumber, checked);
	});

	/*总的点击事件*/
	$(".box").click(function () {
        var checked = 0;
		if ($(this).hasClass("checked")) {
			$(".mz-checkbox").removeClass("checked");
            checked = 0;
		} else {
			$(".mz-checkbox").addClass("checked");
            checked = 1;
		}
		if ($(this).hasClass("checked")) {
			var total = $(".total");
			var sum = 0;
			for (let i = 0; i < total.length; i++) {
				sum += parseInt(total.html());
			}
			$("#totalPrice").html(sum);
		} else {
			$("#totalPrice").html("0.00");
		}
		$(".totalSelectedCount").html($(".checked").length);
        upCartAll(checked);
	});

	/*加减*/
	$(".mz-adder-add").click(function () {
		var pre = $(this).prev().children("input");
		var sub = $(this).prev().prev();
		var value = $(this).prev().children("input").val();
		if (value <= 4) {
			pre.val(++value);
		}
		if (value === 5) {
			$(this).addClass("disabled");
		}

		if (value !== 5 && sub.hasClass("disabled")) {
			sub.removeClass("disabled");
		}
		var cartnumber = $(this).attr("cartnumber");
        var checked = $("#box-" + cartnumber).hasClass("checked") ? 1 : 0;
		var price = $("#price" + cartnumber).html();
		var sum = $("#sum" + cartnumber);
		sum.html(price * (parseInt(value)));
		if ($(".box"+cartnumber).hasClass("checked")) {
			var count = sum.html();
			$("#totalPrice").html(count);
		}

        upCart(value, cartnumber, checked);
	});

	/*减*/
	$(".mz-adder-subtract").click(function () {
		var pre = $(this).next().children("input");
		var add = $(this).next().next();
		var value = $(this).next().children("input").val();

		if (value >= 2) {
			pre.val(--value);
		}

		if (value !== 5 && add.hasClass("disabled")) {
			add.removeClass("disabled");
		}

		if (value === 1) {
			$(this).addClass("disabled");
		}
		var cartnumber = $(this).attr("cartnumber");
        var checked = $("#box-" + cartnumber).hasClass("checked") ? 1 : 0;
		var price = $("#price" + cartnumber).html();
		var sum = $("#sum" + cartnumber);

		sum.html(price * (parseInt(value)));

		if ($(".box"+cartnumber).hasClass("checked")) {
			var count = sum.html();
			$("#totalPrice").html(count);
		}
        upCart(value, cartnumber, checked);
	});

	/*	删除*/
	$("#J-cartHearCtrl").click(function () {
		$("#successoff").show();
		$("#J-cartHearCtrl").hide();
		$("#cart-product").hide();
		$("#cart-remove").show();
	});

	$("#successoff").click(function () {
		$("#J-cartHearCtrl").show();
		$("#successoff").hide();
		$("#cart-product").show();
		$("#cart-remove").hide();
	});
	/*删除弹窗*/
	$(".let-show ").click(function () {
		$(".mz-mask").show();
	});

	/*叉号*/
	$(".mz-dialog-close").click(function () {
		$(".mz-mask").hide();
	});

	/*删除消失*/
	$(".cancel").click(function () {
		$(".mz-mask").hide();
	});

	/*取消删除*/
	$(".success").click(function () {
		$(".mz-mask").hide();
	});
	/*价格变动*/

	/*全选价格变动*/

	function upCart(count, cartnumber, checked) {
		$.ajax({
			url: "/upcart",
			type: "post",
			data: {
				cartnumber: cartnumber,
				count: count,
				checked: checked
			}
		}).done(function (data) {

		});
	}

	function upCartAll(checked) {
		$.ajax({
			url: "/upcart",
			type: "get",
			data: {
				checked: checked
			}
		}).done(function (data) {

		});
	}
});